﻿Imports System.IO
Public Class imageselection
    Shared currentposition As Integer
    Shared imglist As New List(Of Image)
    Shared timglist As New List(Of Image)
    Shared filelist As New List(Of String)
    Public Shared SelectedImagesPathList As New List(Of String)
    ''' <summary>
    ''' Must be the first method to be called. Makes images thunmbnails into your panel from specific folder.
    ''' </summary>
    ''' The directory path, where your images are supposed to be loaded from.
    ''' <param name="DirectoryPath"></param>
    ''' The panel, where you want the images thunbnails to be shown at.
    ''' <param name="YourPanel"></param>
    ''' The form you work with.
    ''' <param name="YourForm"></param>

    Public Sub Start(DirectoryPath As String, YourPanel As Panel, MainPictureBox As PictureBox, YourForm As Form)
        Dim searchpattern As String = "*.jpg;*.jpeg;*.png;*.bmp;*.tif;*.tiff;*.gif"
        Dim Pics As New List(Of PictureBox)
        Dim myint As Integer = 0
        Dim patterns() As String = searchpattern.Split(";"c)
        Try
            For Each ext In patterns
                Dim Files() As String = Directory.GetFiles(DirectoryPath, ext)

                For Each img As String In Files
                    filelist.Add(img)
                    Dim aspectratio As Double = Image.FromFile(img).Height / Image.FromFile(img).Width
                    Dim newwidth As Double = 145 * aspectratio
                    timglist.Add(Image.FromFile(img).GetThumbnailImage(145, newwidth, Nothing, IntPtr.Zero))
                    Dim picbox As New PictureBox
                    picbox.Size = New Size(145, 145)
                    picbox.Image = timglist.Item(myint)
                    picbox.Location = New Point(myint * 160, 0)
                    picbox.SizeMode = PictureBoxSizeMode.Zoom
                    myint = myint + 1
                    YourForm.Controls.Add(picbox)
                    YourPanel.Controls.Add(picbox)
                    Dim myint1 As Integer = myint + 1

                Next

            Next
        Catch ex As OutOfMemoryException
            YourPanel.Visible = False
        End Try
        MainPictureBox.Image = Image.FromFile(filelist.Item(0))
        currentposition = 0
        RIS(MainPictureBox)
    End Sub
    Shared Sub RIS(MainPictureBox As PictureBox)

        If MainPictureBox.Image.Size.Height > MainPictureBox.Size.Height Then
            MainPictureBox.SizeMode = PictureBoxSizeMode.Zoom
        Else
            If MainPictureBox.Image.Size.Width > MainPictureBox.Size.Width Then
                MainPictureBox.SizeMode = PictureBoxSizeMode.Zoom
            Else
                MainPictureBox.SizeMode = PictureBoxSizeMode.CenterImage
            End If
        End If
    End Sub
    ''' <summary>
    ''' Adds the current image to the filtered file list, and goes to the next image.
    ''' </summary>
    ''' The PictureBox with the current image loaded.
    ''' <param name="MainPictureBox"></param>
    ''' The listbox you want to have the list of the selected files.
    ''' <param name="ItemsListBox"></param>
    ''' Determines whether the selected images are copied to directory (originaldirectoryname_chosen) or not.
    ''' <param name="CopyImagesToDirectory"></param>
    Public Shared Sub SelectImageAndGoNext(MainPictureBox As PictureBox, ItemsListBox As ListBox, FolderToCopy As String, Optional CopyImagesToDirectory As Boolean = True)

        If currentposition = filelist.Count - 1 Then
            SelectedImagesPathList.Add(filelist.Item(currentposition))
            Dim fi As New IO.FileInfo(filelist.Item(currentposition))
            ItemsListBox.Items.Add(fi.Name)
            Dim fi2 As New IO.FileInfo(filelist.Item(0))
            If CopyImagesToDirectory = True Then
                For Each sitem In SelectedImagesPathList
                    currentposition = 0
                    Dim rootfolderstr As String = Path.GetDirectoryName(sitem) + "\..\"
                    Dim justfoldername = rootfolderstr + Path.GetFileName(Path.GetDirectoryName(sitem)) + "_chosen"
                    IO.Directory.CreateDirectory(justfoldername)
                    Dim fi1 As New IO.FileInfo(sitem)
                    fi1.CopyTo(justfoldername + "/" + fi1.Name, True)
                Next
            End If
        Else
            Dim fi As New IO.FileInfo(filelist.Item(currentposition))
            ItemsListBox.Items.Add(fi.Name)
            SelectedImagesPathList.Add(filelist.Item(currentposition))
            currentposition = currentposition + 1
            MainPictureBox.Image = Image.FromFile(filelist.Item(currentposition))
            imglist.Add(Image.FromFile(filelist.Item(currentposition)))
        End If
    End Sub
    ''' <summary>
    ''' Don't adds the current image to the filtered file list, and goes to the next image.
    ''' </summary>
    ''' The PictureBox with the current image loaded.
    ''' <param name="MainPictureBox"></param>
    ''' Determines whether the selected images are copied to directory (originaldirectoryname_chosen) or not.
    ''' <param name="CopyImagesToDirectory"></param>
    Private Sub DoNotSelectImageAndGoNext(MainPictureBox As PictureBox, FolderToCopy As String, Optional CopyImagesToDirectory As Boolean = True)
        If currentposition = filelist.Count - 1 Then
            If CopyImagesToDirectory = True Then
                For Each sitem In SelectedImagesPathList
                    currentposition = 0
                    Dim rootfolderstr As String = Path.GetDirectoryName(sitem) + "\..\"
                    Dim justfoldername = rootfolderstr + Path.GetFileName(Path.GetDirectoryName(sitem)) + "_chosen"
                    IO.Directory.CreateDirectory(justfoldername)
                    Dim fi1 As New IO.FileInfo(sitem)
                    fi1.CopyTo(justfoldername + "/" + fi1.Name, True)
                Next
            End If
        Else
            currentposition = currentposition + 1
            MainPictureBox.Image = Image.FromFile(filelist.Item(currentposition))
            imglist.Add(Image.FromFile(filelist.Item(currentposition)))
        End If

    End Sub
End Class
